Welcome back. In this lesson,
we're going to learn about a new Python type called, dictionaries.
Dictionaries like strings, lists,
and tuples are a collection of items.
But unlike strings, lists or tuples,
they're an unordered collection of items meaning that they don't have a first,
second or third item,
they're kind of a bag of key value pairs.
In order to create a dictionary,
we use curly braces.
So, this expression creates an empty dictionary.
We assign this empty dictionary to the variable English to Spanish or eng2sp.
So, that's what line one does.
In order to assign one key value pair to this dictionary, then we say,
eng2sp, and then specify a key that we want,
and then we set it equal to whatever value we want.
So, if I go forward to line two,
you can see that first eng2sp starts out as an empty dictionary,
and then when we run line two,
then we set one key value pair.
So, the key here,
is one, and the value is uno.
Unlike lists, strings or tuples,
dictionaries store key value pairs instead of just items.
What that means, is that every dictionary,
has items that contain one key and one value.
You can think of the key as the thing that you use to actually access the value.
So, for example, in a physical dictionary,
the keys would be words,
and values would be their definitions.
In the context of Python dictionaries,
values can be any Python object,
and keys can be almost any Python object,
but we'll get to that more in a bit.
So here, we have the key one,
associated with the value uno.
That's because in this dictionary, eng2sp,
we're going to associate English words,
with their Spanish equivalent.
We're going to use the English word says
the keys and the Spanish equivalent as the value.
So again, in our code,
we first created an empty dictionary using curly braces and then we assign the value
associated with the key one to be uno by saying eng2sp sub one,
and notice that this is a string because we have quotation marks around it.
So, the key string one,
is the value of string uno.
The next line associates the key two with the value dos,
and you can see that our key value paired here it gets added to the dictionary.
Again, one thing that I mentioned in the introduction
was that dictionaries are unordered,
and that's actually kind of important.
So, there's no notion of what's the first item,
the second item, the third item and so on.
Instead, just think of dictionaries as kind of a bag of key value pairs.
You don't know what order you're going to get them in
but you know if you set two to dos and one to uno,
that these key-value pairs will be
associated with each other no matter what order they're in.
On line four here,
we set the value associated with the key three to be tres.
So, you can see again,
the order kind of changes around in our dictionary,
but the important thing is that every key,
so three is associated with tres,
two is associated with dos,
and one is associated with uno.
Now, when we print out our dictionary,
then we print out a list of key value pairs.
So, here you can tell that this is a dictionary because we have curly braces,
and then every key value pair is separated by a comma.
So, we have two commas here,
and our key value pairs are three,
is associated with tres,
and we can tell because here we have a colon between the key and the value.
Two is associated with dos,
and you can tell, again,
because we have a colon between two and dos,
and one is associated with uno.
So, here we always have the key first and then we have colon and then the value.
Every one of these key value pairs is separated out by commas,
and all of this is wrapped in curly braces to specify that it's a dictionary.
You can also set these key value pairs in line.
So, whereas in this code,
we set one to uno,
on line two and two to dos on line three and so on,
we can also set them all on line one.
So, if we instead declared our dictionary like this,
so here again, we can tell that this is a dictionary because we have curly braces,
and then we have a list of key value pairs.
Every one of these key value pairs is separated out by a comma.
Then we have the key,
so three associated with the value tres and we specify that using a colon.
So, one colon uno, two colon dos.
So, now when we run line one and we look at our frames and objects and we'll see
that eng2sp is now associated with a dictionary that has three key value pairs.
In order to look up the value associated with a particular key,
we use square brackets.
So, here on line one,
we create a dictionary that has three key value pairs,
just like before, and on line three,
we assign value to be eng2sp sub two.
So, in order to get a particular value,
we first say the name of the dictionary,
eng2sp, then we use square brackets.
Then inside of the square brackets,
we put the name of the key that we want to get the value for.
So, here the key is two.
If we look at our dictionary,
we can see the key two is associated with the value dos.
So, the value of this overall expression,
eng2sp sub two is going to be the string dos.
What that means is that when we print out value on line four,
then we're going to print out dos.
So, you can see that running line three and four,
first, on line three,
we assign value to be the string dos,
which is the value of this expression,
and then when we print out value,
in our program output, we get dos.
So, on line five,
we print out eng2sp sub one,
and the value of this expression is the value associated
with the key one in our dictionary eng2sp,
so if I look at the dictionary here,
I can see that the value associated with the key one is uno.
So, the value of this expression is the string uno,
and that's what gets printed when we print out eng2sp, sub one.
That's all for now until next time.